package com.yanqu.road.dao.impl.activity.witchdraw;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.witchdraw.data.WitchDrawNoticeData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WitchDrawNoticeDaoImpl extends TempDao {
    public static String getWitchDrawNoticeDataSql = "select * from `t_c_witch_draw_notice` where `activityId`=? order by `time` desc limit 50";
    public List<WitchDrawNoticeData> getWitchDrawNoticeData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<WitchDrawNoticeData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getWitchDrawNoticeDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parseData(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String insertWitchDrawNoticeDataSql = "insert into `t_c_witch_draw_notice`(activityId,userId,serverId,nickName,drawId,drawType,reward,time) values (?,?,?,?,?,?,?,?)";
    public boolean addNoticeList(List<WitchDrawNoticeData> witchDrawNoticeDataList) {
        boolean result = false;
        getDb().setConnName(DbNameStrategy.getCross2Db());
        List<Map<Integer, DbParameter>> mapList = new ArrayList<Map<Integer, DbParameter>>();
        for (WitchDrawNoticeData witchDrawNoticeData : witchDrawNoticeDataList) {
            Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
            int i = 1;

            para.put(i++, new DbParameter(Types.INTEGER, witchDrawNoticeData.getActivityId()));
            para.put(i++, new DbParameter(Types.BIGINT, witchDrawNoticeData.getUserId()));
            para.put(i++, new DbParameter(Types.BIGINT, witchDrawNoticeData.getServerId()));
            para.put(i++, new DbParameter(Types.VARCHAR, witchDrawNoticeData.getNickName()));
            para.put(i++, new DbParameter(Types.INTEGER, witchDrawNoticeData.getDrawId()));
            para.put(i++, new DbParameter(Types.INTEGER, witchDrawNoticeData.getDrawType()));
            para.put(i++, new DbParameter(Types.VARCHAR, witchDrawNoticeData.getReward()));
            para.put(i++, new DbParameter(Types.BIGINT, witchDrawNoticeData.getTime()));

            mapList.add(para);
        }

        if (getDb().executeBatchQuery(insertWitchDrawNoticeDataSql, mapList) > -1) {
            result = true;
        }
        return result;
    }


    protected WitchDrawNoticeData parseData(ResultSet rs) throws SQLException {
        WitchDrawNoticeData witchDrawNoticeData = new WitchDrawNoticeData();
        witchDrawNoticeData.setActivityId(rs.getInt("activityId"));
        witchDrawNoticeData.setUserId(rs.getLong("userId"));
        witchDrawNoticeData.setServerId(rs.getLong("serverId"));
        witchDrawNoticeData.setNickName(rs.getString("nickName"));
        witchDrawNoticeData.setDrawId(rs.getInt("drawId"));
        witchDrawNoticeData.setDrawType(rs.getInt("drawType"));
        witchDrawNoticeData.setReward(rs.getString("reward"));
        witchDrawNoticeData.setTime(rs.getLong("time"));

        return witchDrawNoticeData;
    }


}
